package org.qloudgen.borg.pages;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.qloudgen.borg.CommonValues;
import org.qloudgen.borg.beans.Cluster;
import org.qloudgen.borg.metadb.DefaultImpl;
import org.qloudgen.borg.pages.common.BasePage;

public class ClusterAddPage extends BasePage{
	
	/**
	 * @Author youngeryoyo
	 * youngeryoyo@gmail.com
	 */
	static Log log = LogFactory.getLog(ClusterAddPage.class);
	
	private static final long serialVersionUID = 1L;
	private transient Cluster cluster;
	
	public ClusterAddPage(PageParameters pageParameters){
		super();
		
		this.pp = pageParameters;
	
		// form
		final Form<Object> form = new Form<Object>("form") {
			
			private static final long serialVersionUID = 1L;
			protected void onSubmit() {
				
				cluster = (Cluster)getDefaultModelObject();
				
				// check cluster info 
				String checkInfo = cluster.checkClusterInfo(true);
				
				if (StringUtils.isNotEmpty(checkInfo)) {
					
					info(checkInfo);
					
				} else {
					
					cluster.setClusterID(String.valueOf(DefaultImpl.getClusterID())).setClusterStatus("on").setClusterType("add").setIsAvailable("y");
					
					DefaultImpl.createCluster(cluster);
					setResponsePage(ClusterListPage.class,pp);
				
				}
			}
			
		};
		form.setDefaultModel(new CompoundPropertyModel<Cluster>(new Cluster().setScriptPath(CommonValues.DEFAULT_HADOOP_SCRIPT_PATH)
				                                                             .setDataPath(CommonValues.DEFAULT_HADDOP_DATA_PATH)
				                                                             .setJarPath(CommonValues.DEFAULT_HADDOP_JAR_PATH)));
		this.add(form);
		
		// form.(clusterName,clusterDesc,controllerIp,sysUserPassword,rootPassword,scriptPath)
		form.add(new RequiredTextField<String>("clusterName"));
		form.add(new TextField<String>("clusterDesc"));
		form.add(new RequiredTextField<String>("clusterInfo").setVisible(false));
		form.add(new RequiredTextField<String>("masterIp"));
		form.add(new RequiredTextField<String>("sysUser"));
		form.add(new PasswordTextField("sysUserPassword").setRequired(true));
		form.add(new RequiredTextField<String>("scriptPath"));
		form.add(new RequiredTextField<String>("dataPath"));
		form.add(new RequiredTextField<String>("jarPath"));
		form.add(new TextField<String>("maxTasks"));
		
		form.add(new DropDownChoice<String>("version", CommonValues.hadoopVersions).setNullValid(false).setRequired(true));		
		
		form.add(new Button ("submit"));
		
		Button cancel = new Button ("cancel"){
			private static final long serialVersionUID = 1L;
			public void onSubmit() {
				setResponsePage(ClusterListPage.class,pp);
			}
		};
		form.add(cancel);
		cancel.setDefaultFormProcessing(false);
		
	}

}
